package com.szholly.plug.safe.directive;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.szholly.utils.freemarker.directive.IDirective;
import com.szholly.utils.session.SessionFactory;
import com.szholly.utils.session.provider.ISessionProvider;
import com.szholly.utils.spring.springmvc.route.FunctionInfo;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 定义当前登录用户名
 */
@Component("directive_userrealname")
public class CurUserRealNameDirective implements TemplateDirectiveModel,
		IDirective {

	@SuppressWarnings({ "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {

		Object userName = null;
		ISessionProvider session = SessionFactory.getSession();
		if (session != null) {
			userName = session.getObject(ISessionProvider.UserRealName);
		}
		String userNameString = "";
		if (userName != null) {
			userNameString = userName.toString();
		}

		Writer out = env.getOut();
		out.append(userNameString);
	}

	@Override
	public FunctionInfo getFunctionInfo() {
		String paraInfo = "" + "示例：<@userrealname  />";
		FunctionInfo info = new FunctionInfo("userrealname", 1, "ͨ基础", "当前登录用户名标签",
				paraInfo);
		return info;
	}
}
